package com.zhoutao.blog.seven.service.impl;

import com.zhoutao.blog.seven.model.Cacheable;
import com.zhoutao.blog.seven.service.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class CacheServiceImpl implements CacheService, ApplicationContextAware {

    private ApplicationContext applicationContext;


    @Override
    public void clean() {
        Map<String, Cacheable> beans = this.applicationContext.getBeansOfType(Cacheable.class);
        for (Cacheable value : beans.values()) {
            value.cleanCache();
            log.info("清理缓存完成:{}", value.getClass().getName());
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
